/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.web.command.component.serializer.CommandItem;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.inspur.edp.caf.cef.schema.base.utils.JsonUtil;
import com.inspur.edp.web.command.component.ComponentConstant;
import com.inspur.edp.web.command.component.metadata.BranchCommandItem;
import com.inspur.edp.web.command.component.metadata.CommandItem;
import com.inspur.edp.web.command.component.metadata.ConditionType;
import com.inspur.edp.web.command.component.serializer.CommandItemSerializer;

import java.util.List;

/**
 * @author Kaixuan Shi
 * @version 0.1
 */
public class BranchCommandItemSerializer implements ICommandItemSerializer {
    @Override
    public JsonNode serialize(CommandItem item) {
        JsonNode itemNode = (new ObjectMapper()).createObjectNode();
        BranchCommandItem branchCommandItem = (BranchCommandItem) item;
        JsonUtil.setPropValue(itemNode, ComponentConstant.CommandItemField.ITEM_TYPE, branchCommandItem.getItemType());
        JsonUtil.setPropValue(itemNode, ComponentConstant.BranchCommandItemField.ID, branchCommandItem.getId());
        JsonUtil.setPropValue(itemNode, ComponentConstant.BranchCommandItemField.CODE, branchCommandItem.getCode());
        JsonUtil.setPropValue(itemNode, ComponentConstant.BranchCommandItemField.NAME, branchCommandItem.getName());
        JsonUtil.setPropValue(itemNode, ComponentConstant.BranchCommandItemField.CONDITION_TYPE, branchCommandItem.getConditionType());
        JsonUtil.setPropValue(itemNode, ComponentConstant.BranchCommandItemField.EXPRESS, branchCommandItem.getExpress());
        List<CommandItem> commandItems = branchCommandItem.getItems();
        if (commandItems == null || commandItems.isEmpty()) {
            return itemNode;
        }
        ArrayNode itemsArrayNode = JsonUtil.addEmptyArray(itemNode, ComponentConstant.BranchCommandItemField.ITEMS);
        for (CommandItem commandItem : commandItems) {
            JsonNode commandItemNode = CommandItemSerializer.serialize(commandItem);
            itemsArrayNode.add(commandItemNode);
        }
        return itemNode;
    }

    @Override
    public CommandItem deserialize(JsonNode itemNode) {
        BranchCommandItem branchCommandItem = new BranchCommandItem();
        branchCommandItem.setId(JsonUtil.getPropValue(itemNode, ComponentConstant.BranchCommandItemField.ID, String.class));
        branchCommandItem.setCode(JsonUtil.getPropValue(itemNode, ComponentConstant.BranchCommandItemField.CODE, String.class));
        branchCommandItem.setName(JsonUtil.getPropValue(itemNode, ComponentConstant.BranchCommandItemField.NAME, String.class));
        branchCommandItem.setConditionType(JsonUtil.getPropValue(itemNode, ComponentConstant.BranchCommandItemField.CONDITION_TYPE, ConditionType.class));
        branchCommandItem.setExpress(JsonUtil.getPropValue(itemNode, ComponentConstant.BranchCommandItemField.EXPRESS, String.class));
        ArrayNode itemsArrayNode = JsonUtil.getPropValue(itemNode, ComponentConstant.BranchCommandItemField.ITEMS, ArrayNode.class);
        if (itemsArrayNode == null || itemsArrayNode.isEmpty()) {
            return branchCommandItem;
        }
        List<CommandItem> commandItems = branchCommandItem.getItems();
        for (JsonNode commandItemNode : itemsArrayNode) {
            CommandItem item = CommandItemSerializer.deserialize(commandItemNode);
            commandItems.add(item);
        }
        return branchCommandItem;
    }
}
